const db = require('../config/config')
module.exports = {
    // 查询热门数据
    test(req, res) {
        let sql = `select * from task_all `
        db.dbMysql(sql, [], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }
        })
    },
    searchAdmin(req, res) {
        let kerWord = req.body.keyWord
        let sql = `select * from u_user where u_name like \'%${kerWord}%\' or u_id like \'%${kerWord}%\' or u_account like \'%${kerWord}%\' and u_isdel=0`

        db.dbMysql(sql, [kerWord], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    data: data,
                    msg: '查询成功'
                })
            }
        })
    },
    yanZ(req, res) {
        let account = req.body.u_account
        db.dbMysql('select * from u_user where u_account=?',
            [account], function (err, data) {
                if (data.length == 0) {
                    res.send({
                        code: 200,
                        msg: "可用账号"
                    })
                } else {
                    res.send({
                        code: 201,
                        msg: '该账户已存在'
                    })
                }
            })
    },
    addAdmin(req, res) {
        let u_name = req.body.u_name;
        let u_account = req.body.u_account;
        let u_password = req.body.u_password;
        let sql = `insert into u_user(u_name,u_account,u_password) values (${u_name},${u_account},${u_password})`
        db.dbMysql(sql, [], function (err, data) {
            if (!err) {
                res.send({
                    code: 200,
                    data: data
                })
            } else {
                res.send({
                    code: 201,
                    err: err
                })
            }
        })
    },

    getAdmin(req, res) {
        db.dbMysql('select * from u_user where u_isdel=0',
            [], function (err, data) {
                if (!err) {
                    res.send({
                        code: 200,
                        data: data
                    })
                } else {
                    res.send({
                        code: 201,
                        err: err
                    })
                }
            })
    },
    getAdminOne(req, res) {
        db.dbMysql('select * from u_user where u_id=?',
            [req.body.u_id], function (err, data) {
                if (!err) {
                    res.send({
                        code: 200,
                        data: data
                    })
                } else {
                    res.send({
                        code: 201,
                        err: err
                    })
                }
            })
    },
    // 假删
    delAdmin(req, res) {
        let u_id = req.body.u_id
        db.dbMysql('update u_user set u_isdel=1 where u_id=?', [u_id], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    msg: '删除成功'
                })
            }
        })
    },
    // 修改
    upDataAdmin(req, res) {
        let obj = req.body
        let u_id = obj.u_id;
        let u_name = obj.u_name;
        let u_password = obj.u_password;
        let u_type = obj.u_type;
        let u_account = obj.u_account;
        let u_isdel = obj.u_isdel
        db.dbMysql('update u_user set u_name=?,u_password=?,u_type=?,u_account=?, u_isdel=? where u_id=?', [u_name, u_password, u_type, u_account, u_isdel, u_id], function (err, data) {
            if (data) {
                res.send({
                    code: 200,
                    msg: '修改成功'
                })
            }
        })

    }
}